package com.castle.fortress.admin.study.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.annotation.CastleLog;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.study.exam.dto.StudyQuestionbankDto;
import com.castle.fortress.admin.study.exam.entity.StudyExamQuestionEntity;
import com.castle.fortress.admin.study.exam.entity.StudyQuestionEntity;
import com.castle.fortress.admin.study.exam.entity.StudyQuestionbankEntity;
import com.castle.fortress.admin.study.exam.service.StudyExamQuestionService;
import com.castle.fortress.admin.study.exam.service.StudyQuestionService;
import com.castle.fortress.admin.study.exam.service.StudyQuestionbankService;
import com.castle.fortress.admin.utils.ExcelUtils;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.common.enums.YesNoEnum;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.BizErrorCode;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 题库管理 控制器
 *
 * @author castle
 * @since 2023-02-09
 */
@Api(tags="题库管理管理控制器")
@Controller
public class StudyQuestionbankController {
    @Autowired
    private StudyQuestionbankService studyQuestionbankService;
    @Autowired
    private StudyExamQuestionService studyExamQuestionService;
    @Autowired
    private StudyQuestionService studyQuestionService;

    /**
     * 题库管理的分页展示
     * @param studyQuestionbankDto 题库管理实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="题库管理",operType = OperationTypeEnum.QUERY)
    @ApiOperation("题库管理分页展示")
    @GetMapping("/study/exam/studyQuestionbank/page")
    @ResponseBody
    @RequiresPermissions("study:exam:studyQuestionbank:pageList")
    public RespBody<IPage<StudyQuestionbankDto>> pageStudyQuestionbank(StudyQuestionbankDto studyQuestionbankDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<StudyQuestionbankDto> page = new Page(pageIndex, pageSize);
        IPage<StudyQuestionbankDto> pages = studyQuestionbankService.pageStudyQuestionbankExtends(page, studyQuestionbankDto);

        return RespBody.data(pages);
    }

    /**
     * 题库管理的列表展示
     * @param studyQuestionbankDto 题库管理实体类
     * @return
     */
    @CastleLog(operLocation="题库管理",operType = OperationTypeEnum.QUERY)
    @ApiOperation("题库管理列表展示")
    @GetMapping("/study/exam/studyQuestionbank/list")
    @ResponseBody
    public RespBody<List<StudyQuestionbankDto>> listStudyQuestionbank(StudyQuestionbankDto studyQuestionbankDto){
        List<StudyQuestionbankDto> list = studyQuestionbankService.listStudyQuestionbank(studyQuestionbankDto);
        return RespBody.data(list);
    }

    /**
     * 题库管理保存
     * @param studyQuestionbankDto 题库管理实体类
     * @return
     */
    @CastleLog(operLocation="题库管理",operType = OperationTypeEnum.INSERT)
    @ApiOperation("题库管理保存")
    @PostMapping("/study/exam/studyQuestionbank/save")
    @ResponseBody
    @RequiresPermissions("study:exam:studyQuestionbank:save")
    public RespBody<String> saveStudyQuestionbank(@RequestBody StudyQuestionbankDto studyQuestionbankDto){
        if(studyQuestionbankDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyQuestionbankEntity studyQuestionbankEntity = ConvertUtil.transformObj(studyQuestionbankDto,StudyQuestionbankEntity.class);
        if(studyQuestionbankService.checkName(studyQuestionbankEntity)){
            throw new BizException(BizErrorCode.NAME_LEVEL_EXIST_ERROR);
        }
        studyQuestionbankEntity.setStatus(YesNoEnum.NO.getCode());//未开放
        if(studyQuestionbankService.save(studyQuestionbankEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 题库管理编辑
     * @param studyQuestionbankDto 题库管理实体类
     * @return
     */
    @CastleLog(operLocation="题库管理",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("题库管理编辑")
    @PostMapping("/study/exam/studyQuestionbank/edit")
    @ResponseBody
    @RequiresPermissions("study:exam:studyQuestionbank:edit")
    public RespBody<String> updateStudyQuestionbank(@RequestBody StudyQuestionbankDto studyQuestionbankDto){
        if(studyQuestionbankDto == null || studyQuestionbankDto.getId() == null || studyQuestionbankDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyQuestionbankEntity studyQuestionbankEntity = ConvertUtil.transformObj(studyQuestionbankDto,StudyQuestionbankEntity.class);
        if(studyQuestionbankService.checkName(studyQuestionbankEntity)){
            throw new BizException(BizErrorCode.NAME_LEVEL_EXIST_ERROR);
        }
        if(studyQuestionbankService.updateById(studyQuestionbankEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 题库管理删除
     * @param id
     * @return
     */
    @CastleLog(operLocation="题库管理",operType = OperationTypeEnum.DELETE)
    @ApiOperation("题库管理删除")
    @PostMapping("/study/exam/studyQuestionbank/delete")
    @ResponseBody
    @RequiresPermissions("study:exam:studyQuestionbank:delete")
    public RespBody<String> deleteStudyQuestionbank(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        //校验是否绑定试卷
        QueryWrapper<StudyExamQuestionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("qb_id",id);
        List<StudyExamQuestionEntity> list = studyExamQuestionService.list(wrapper);
        if(list!=null && list.size()>0){
            throw new BizException(BizErrorCode.QUESTIONBANK_BIND_DEL_ERROR);
        }
        if(studyQuestionbankService.removeById(id)) {
            //删除题目
            QueryWrapper<StudyQuestionEntity> delWrapper = new QueryWrapper<>();
            delWrapper.eq("qb_id",id);
            studyQuestionService.remove(delWrapper);
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


//    /**
//     * 题库管理批量删除
//     * @param ids
//     * @return
//     */
//    @CastleLog(operLocation="题库管理",operType = OperationTypeEnum.DELETE)
//    @ApiOperation("题库管理批量删除")
//    @PostMapping("/study/exam/studyQuestionbank/deleteBatch")
//    @ResponseBody
//    @RequiresPermissions("study:exam:studyQuestionbank:deleteBatch")
//    public RespBody<String> deleteStudyQuestionbankBatch(@RequestBody List<Long> ids){
//        if(ids == null || ids.size()<1){
//            throw new BizException(GlobalRespCode.PARAM_MISSED);
//        }
//        if(studyQuestionbankService.removeByIds(ids)) {
//            return RespBody.data("保存成功");
//        }else{
//            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
//        }
//    }

    /**
     * 题库管理详情
     * @param id 题库管理id
     * @return
     */
    @CastleLog(operLocation="题库管理",operType = OperationTypeEnum.QUERY)
    @ApiOperation("题库管理详情")
    @GetMapping("/study/exam/studyQuestionbank/info")
    @ResponseBody
    @RequiresPermissions("study:exam:studyQuestionbank:info")
    public RespBody<StudyQuestionbankDto> infoStudyQuestionbank(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyQuestionbankDto studyQuestionbankDto =  studyQuestionbankService.getByIdExtends(id);

        return RespBody.data(studyQuestionbankDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation="题库管理",operType = OperationTypeEnum.EXPORT)
	@PostMapping("/study/exam/studyQuestionbank/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<StudyQuestionbankDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<StudyQuestionbankDto> list = studyQuestionbankService.listStudyQuestionbank(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
